//
//  DFNavTitleView.swift
//  DailyFresh
//
//  Created by Arthur on 2017/9/21.
//  Copyright © 2017年 DailyFresh. All rights reserved.
//

import UIKit

let navTitleWidth = 172.0

@objc protocol DFNavTitleViewDelegate {
    func navTitle(tap: DFNavTitleView)
}


extension DFNavTitleView {
    public func update(iconString: String = "jisuda_55x14_", titleString: String = "九江九江九江") {
        self.icon.image = UIImage.init(named: iconString)
        self.title.text = titleString
    }
}

class DFNavTitleView: UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.white
        self.icon.frame = CGRect.init(x: 0, y: 0, width: 55, height: 14)
        self.addSubview(self.icon)
        self.title.frame = CGRect.init(x: 60, y: 0, width: 70, height: 44)
        self.addSubview(self.title)
        self.arrowDown.frame = CGRect.init(x: self.title.right + 10, y: 0, width: 10, height: 5)
        self.addSubview(self.arrowDown)
        
        self.arrowDown.centerY = self.centerY
        self.icon.centerY = self.centerY
        self.title.centerY = self.centerY
//        self.backgroundColor = UIColor.red
//        self.addSubview(self.icon)
//        self.addSubview(self.title)
//        //        self.addSubview(arrowDown)
//
//        self.icon.snp.makeConstraints { (make) in
//            make.width.equalTo(normalize(value: 55))
//            make.height.equalTo(normalize(value: 14))
//            make.left.equalTo(0)
//            make.centerY.equalTo(self)
//        }
//
//        self.title.snp.makeConstraints { (make) in
//            make.width.equalTo(normalize(value: 80))
//            make.height.equalTo(DFLayoutConstants.navigationbarHeigth)
//            make.left.equalTo(self.icon.snp.right).offset(10)
//            make.centerY.equalTo(self)
//        }
//
        let tap: UITapGestureRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(navTitleTaped))
        self.addGestureRecognizer(tap)
    }
    
    
    
    weak var delegate: DFNavTitleViewDelegate?
    lazy var icon: UIImageView = {
        [unowned self] in
        let tmp = UIImageView()
        return tmp
        }()
    
    lazy var title: UILabel = {
        [unowned self] in
        let tmp = UILabel()
        tmp.font = UIFont.systemFont(ofSize: normalize(value: 13))
        tmp.textAlignment = .center
        
        return tmp
        }()
    
    lazy var arrowDown: UIImageView = {
        [unowned self] in
        let tmp = UIImageView()
        tmp.image = #imageLiteral(resourceName: "order_down_arrow_black_10x5_")
        return tmp
        }()
    
    
    @objc func navTitleTaped()  {
        self.delegate?.navTitle(tap: self)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
